package com.genymobile.scrcpy.five.cmd;

import android.view.MotionEvent;

import com.genymobile.scrcpy.five.inject.FiveInjector;

public class FiveMotionEventCmd extends ICmd {
    public int displayId;
    public int fingerId;
    public int action;
    public float pressure;
    public int x;
    public int y;
    public boolean downReported = false;

    @Override
    public boolean run(FiveInjector injector) {
        return injector.injectTouch(action, fingerId, x, y, pressure, 0, 0, displayId);
    }

    @Override
    public String toString() {
        return super.toString() + displayId +
                " " + fingerId +
                " " + action +
                " " + pressure +
                " " + x +
                " " + y +
                " " + getExtraString();
    }

    private String getExtraString() {
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                return "#### motion down";
            case MotionEvent.ACTION_MOVE:
                return "#### motion move";
            case MotionEvent.ACTION_UP:
                return "#### motion up";
        }
        return "";
    }

    @Override
    String getCmdName() {
        return "motion";
    }

    // 解析方法
    @Override
    public ICmd parse(long timestampMs, String[] arr) {

        // 创建一个 OneEvent 对象
        FiveMotionEventCmd event = new FiveMotionEventCmd();
        event.timestampMs = timestampMs;

        // 将分割的字符串解析为对应的字段
        int i = 0;
        event.displayId = Integer.parseInt(arr[i++]);
        event.fingerId = Integer.parseInt(arr[i++]);
        event.action = Integer.parseInt(arr[i++]);
        event.pressure = Float.parseFloat(arr[i++]);
        event.x = Integer.parseInt(arr[i++]);
        event.y = Integer.parseInt(arr[i++]);

        return event;
    }

    public static FiveMotionEventCmd convertMotionEventToOneEvent(long timestampMs, MotionEvent motionEvent, int displayId) {
        // 创建一个新的 OneEvent 对象
        FiveMotionEventCmd event = new FiveMotionEventCmd();

        event.timestampMs = timestampMs;

        // 从 MotionEvent 中提取字段，赋值到 OneEvent
        event.displayId = (int) displayId;                 // 获取 Y 坐标，转为 int
        event.action = motionEvent.getAction();             // 获取 Action 类型
        event.pressure = motionEvent.getPressure();         // 获取压力值
        event.x = (int) motionEvent.getX();                 // 获取 X 坐标，转为 int
        event.y = (int) motionEvent.getY();                 // 获取 Y 坐标，转为 int

        return event;
    }
}
